package yf.mall.dysdk.model.request.video;

import com.alibaba.fastjson.JSONObject;
import com.yf.response.RRException;
import com.yf.utils.HttpTookit;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import yf.mall.utils.StringUtil;
import yf.mall.vo.app.dy.DyVideoVO;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 付费的接口
 * https://eeapi.cn/
 */
@Slf4j
public class DyVideoRequest0 extends DyVideoBaseRequest {


    public DyVideoRequest0() {
        this.url = "https://eeapi.cn/api/video/32927EFC0523C85BD8500DB17D052C1F2C6C7523537393496D/4153/?url=";
        this.method = HttpMethod.GET;
    }


    @SneakyThrows
    @Override
    public DyVideoVO parseVideo(String shareVideo) {
//        this.url = this.url + getShareUrl(shareVideo);
        String url = this.url + getShareUrl(shareVideo);
        Long current = System.currentTimeMillis();
        String res = HttpTookit.doGet(url,null);
        current = System.currentTimeMillis() - current;
        log.info("请求去水印接口,url:{},res:{}", url, res);
        if (StringUtil.isBlank(res)) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
        JSONObject jsonObject = JSONObject.parseObject(res);
        if (!jsonObject.get("code").equals(200) || !jsonObject.containsKey("data")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
        JSONObject data = jsonObject.getJSONObject("data");
//        if (!data.containsKey("down")) {
//            log.error("请求去水印接口失败,url:{},res:{}", url, res);
//        }
        return new DyVideoVO(data.getString("down"), null, data.getString("cover"), data.getString("title"),current,data.getObject("images", List.class));
    }
}
